package organizer;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 * Contains all the rules for selecting which files to delete.
 * Files are selected based on priority of parent directories.
 * @author dawong
 */
public class RulesComponent extends JPanel {

    private JTable rulesTable;

    private DefaultTableModel rulesTableModel;

    public RulesComponent() {
        rulesTableModel = new DefaultTableModel(new String[] { "Parent Directory" }, 0) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

            private Class[] COLUMN_CLASS = { String.class };
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return COLUMN_CLASS[columnIndex];
            }

        };
        initComponents();
    }

    private void initComponents() {
        JPanel contentPane = this;
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints c;

        rulesTable = new JTable(rulesTableModel);
        rulesTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        c = new GridBagConstraints();
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.REMAINDER;
        contentPane.add(new JScrollPane(rulesTable), c);

        JPanel buttonPane = new JPanel();
        c = new GridBagConstraints();
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = GridBagConstraints.REMAINDER;
        contentPane.add(buttonPane, c);

        JButton moveUpButton = new JButton("Up");
        moveUpButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                moveUpButtonActionPerformed();
            }
        });
        c = new GridBagConstraints();
        c.weightx = 1;
        c.anchor = GridBagConstraints.CENTER;
        buttonPane.add(moveUpButton, c);

        JButton moveDownButton = new JButton("Down");
        moveDownButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                moveDownButtonActionPerformed();
            }
        });
        c = new GridBagConstraints();
        c.weightx = 1;
        c.anchor = GridBagConstraints.CENTER;
        buttonPane.add(moveDownButton, c);

        JButton moveBottomButton = new JButton("Bottom");
        moveBottomButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                moveBottomButtonActionPerformed();
            }
        });
        c = new GridBagConstraints();
        c.weightx = 1;
        c.anchor = GridBagConstraints.CENTER;
        buttonPane.add(moveBottomButton, c);

        JButton moveTopButton = new JButton("Top");
        moveTopButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                moveTopButtonActionPerformed();
            }
        });
        c = new GridBagConstraints();
        c.weightx = 1;
        c.anchor = GridBagConstraints.CENTER;
        buttonPane.add(moveTopButton, c);

        JButton applyButton = new JButton("Apply");
        applyButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
                ActionEvent newEvent = new ActionEvent(this, 0, "apply");
                for (ActionListener l : listeners) {
                    l.actionPerformed(newEvent);
                }
            }
        });
        c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        contentPane.add(applyButton, c);
    }
    
    private void moveTopButtonActionPerformed() {
        ListSelectionModel selectModel = rulesTable.getSelectionModel();
        int start = selectModel.getMinSelectionIndex();
        int end = selectModel.getMaxSelectionIndex();
        int newStart = 0;
        int newEnd = newStart + end - start;
        rulesTableModel.moveRow(start, end, newStart);
        selectModel.setSelectionInterval(newStart, newEnd);
    }

    private void moveBottomButtonActionPerformed() {
        ListSelectionModel selectModel = rulesTable.getSelectionModel();
        int start = selectModel.getMinSelectionIndex();
        int end = selectModel.getMaxSelectionIndex();
        int newStart = rulesTable.getRowCount()-1-(end-start);
        int newEnd = newStart + end - start;
        rulesTableModel.moveRow(start, end, newStart);
        selectModel.setSelectionInterval(newStart, newEnd);
    }
    
    private void moveDownButtonActionPerformed() {
        ListSelectionModel selectModel = rulesTable.getSelectionModel();
        int start = selectModel.getMinSelectionIndex();
        int end = selectModel.getMaxSelectionIndex();
        int newStart = Math.min(rulesTable.getRowCount()-1, start+1);
        int newEnd = newStart + end - start;
        rulesTableModel.moveRow(start, end, newStart);
        selectModel.setSelectionInterval(newStart, newEnd);
    }

    private void moveUpButtonActionPerformed() {
        ListSelectionModel selectModel = rulesTable.getSelectionModel();
        int start = selectModel.getMinSelectionIndex();
        int end = selectModel.getMaxSelectionIndex();
        int newStart = Math.max(0, start - 1);
        int newEnd = newStart + end - start;
        rulesTableModel.moveRow(start, end, newStart);
        selectModel.setSelectionInterval(newStart, newEnd);
    }

    public void addActionListener(ActionListener l) {
        listenerList.add(ActionListener.class, l);
    }

    public void removeActionListener(ActionListener l) {
        listenerList.remove(ActionListener.class, l);
    }

    public void setDirectories(List<String> dirs) {
        rulesTableModel.setRowCount(0);
        for (String dir : dirs) {
            rulesTableModel.addRow(new Object[] { dir });
        }
    }

    public List<String> getDirectories() {
        List<String> ret = new ArrayList<String>();
        int size = rulesTableModel.getRowCount();
        for (int r = 0; r < size; r++) {
            String dir = (String) rulesTableModel.getValueAt(r, 0);
            ret.add(dir);
        }
        return ret;
    }

}
